javascipt 可能会抛出异常的方法统计
此文档来自对
mdn Errors参考页面
的整理。
总得来说使用下面这些需要小心谨慎
构造相关
Array
ArrayBuffer
Date
Function
RegExp
方法调用相关
String
String.fromCodePoint()
String.prototype.repeat()
Date.parse()
Number
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toPrecision()
Number.prototype.toString()
JSON
JSON.parse()
JSON.stringify()
Object.create()
Symbol.keyFor()
instanceof
URI
decodeURI()
encodeURI()
encodeURIComponent()
decodeURIComponent()
Function
Function.prototype.call()
Function.prototype.apply()
特殊场景下的错误
Error: Permission denied to access property "x"
InternalError: too much recursion
ReferenceError
RangeError
String.fromCodePoint()
RangeError: argument is not a valid code point
Array
ArrayBuffer
RangeError: invalid array length
Date
Date.parse()
RangeError: invalid date
Number.prototype.toExponential()
Number.prototype.toFixed()
Number.prototype.toPrecision()
RangeError: precision is out of range
Number.prototype.toString()
RangeError: radix must be an integer
String.prototype.repeat()
RangeError: repeat count must be less than infinity
用来计数的参数小于正
Infinity
,
且不能为负数
。该值的合法范围可以这样表示: [0, +∞)。
其结果字符串也不能长于最大字符串,不同 JavaScript 引擎中可能有所不同。 在 Firefox (SpiderMonkey) 里最大字符串大小为 2
28
-1 (
0xFFFFFFF
)。
String.prototype.repeat()
RangeError: repeat count must be non-negative
ReferenceError
ReferenceError: "x" is not defined
当你使用变量的时候,这个变量必须是已经被声明的,或者你可以确保它在你当前的脚本或作用域 (
scope
) 中可用。
ReferenceError: assignment to undeclared variable "x"
仅在
严格模式
中出现
ReferenceError
警告。
ReferenceError: can't access lexical declaration`X' before initialization
词法变量在初始化之前被访问。该错误可以发生于任何语句块中,当使用 let 或 const 修饰的变量在初始化之前被访问的时候。
ReferenceError: deprecated caller or arguments usage
仅在严格模式下出现的
ReferenceError
警告。JavaScript 的执行将不会停止。
在
strict mode
中,
Function.caller
和
Function.arguments
属性是不该使用的。它们都是已经被废弃的了,因为这两者泄露了函数的调用者,是不标准的,难于优化和有这潜在的性能问题。
ReferenceError: invalid assignment left-hand side
ReferenceError: reference to undefined property "x"
脚本尝试去访问一个不存在的对象属性。
property accessors
页面描述了两种访问属性的方法。
引用未定义属性的错误仅出现在
strict mode
代码中。在非严格代码中,对不存在的属性的访问将被忽略
SyntaxError
SyntaxError: "0"-prefixed octal literals and octal escape seq. are deprecated
SyntaxError: "use strict" not allowed in function with non-simple parameters
SyntaxError: "x" is a reserved identifier
保留字
用作标记符将会出错
SyntaxError: "x" is not a legal ECMA-262 octal constant
十进制字面量可以以零作为开始(
0
),后面跟着其他十进制数,但是假如前导 0 之后的所有数字都小于 8,那么这个数就会被解析为一个八进制的数。因为 08 和 09 不是这样的,所以 JavaScript 会发出警告
JSON.parse()
SyntaxError: JSON.parse: bad parsing
Function
SyntaxError: Malformed formal parameter
SyntaxError: Unexpected token
SyntaxError: Using //@ to indicate sourceURL pragmas is deprecated. Use //# instead
SyntaxError: a declaration in the head of a for-of loop can
t have an initializer
SyntaxError: applying the
delete
operator to an unqualified name is deprecated
SyntaxError: for-in loop head declarations may not have initializers
SyntaxError: function statement requires a name
SyntaxError: identifier starts immediately after numeric literal
SyntaxError: illegal character
RegExp
SyntaxError: invalid regular expression flag "x"
SyntaxError: missing ) after argument list
SyntaxError: missing ) after condition
SyntaxError: missing : after property id
SyntaxError: missing ; before statement
SyntaxError: missing = in const declaration
SyntaxError: missing
after element list
SyntaxError: missing formal parameter
SyntaxError: missing name after . operator
SyntaxError: missing variable name
SyntaxError: missing } after function body
SyntaxError: missing } after property list
SyntaxError: redeclaration of formal parameter "x"
SyntaxError: return not in function
SyntaxError: test for equality (==) mistyped as assignment (=)?
SyntaxError: unterminated string literal
TypeError: "x" has no properties
Object.create()
Symbol.keyFor()
TypeError: "x" is (not) "y"
TypeError: "x" is not a constructor
TypeError: "x" is not a function
TypeError: "x" is not a non-null object
TypeError: "x" is read-only
TypeError: 'x' is not iterable
TypeError: More arguments needed
TypeError: Reduce of empty array with no initial value
TypeError: can't access dead object
TypeError: can't access property "x" of "y"
TypeError: can't assign to property "x" on "y": not an object
TypeError: can't define property "x": "obj" is not extensible
TypeError: can't delete non-configurable array element
TypeError: can't redefine non-configurable property "x"
JSON.stringify()
TypeError: cyclic object value
TypeError: invalid 'in' operand "x"
TypeError: invalid 'instanceof' operand 'x'
TypeError: invalid Array.prototype.sort argument
instanceof
TypeError: invalid arguments
TypeError: invalid assignment to const "x"
TypeError: property "x" is non-configurable and can't be deleted
TypeError: setting getter-only property "x"
TypeError: variable "x" redeclares argument
decodeURI()
encodeURI()
encodeURIComponent()
decodeURIComponent()
URIError: malformed URI sequence
Warning: -file- is being assigned a //# sourceMappingURL, but already has one
Date.prototype.toLocaleFormat
Warning: Date.prototype.toLocaleFormat is deprecated
toLocaleFormat 已经弃用了
Warning: JavaScript 1.6's for-each-in loops are deprecated
Warning: String.x is deprecated; use String.prototype.x instead
Warning: expression closures are deprecated
Warning: unreachable code after return statement
Function.prototype.call()
Function.prototype.apply()
X.prototype.y called on incompatible type
相关文档
mdn Errors参考页面